package com.veryUsedCar.controller;

import com.veryUsedCar.data.Result;

import com.veryUsedCar.entity.Conllection;
import com.veryUsedCar.service.CollectionService;
import com.veryUsedCar.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author  woo
 * date 2022/11/18 15:48
 * 描述：
 */
@RestController
@RefreshScope  // 刷新配置
@RequestMapping("collection")
@Slf4j
@Api("收藏页面展示接口") // (代表着controller层是一个什么接口)
public class CollectionController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private CollectionService collectionService;

    // 获取 登录状态
    @PostMapping("/token")
    @ApiOperation(value = "获取登录token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token",value = "token",required = true, paramType = "header")
    })
    public Result getToken(@RequestHeader("token") String token){
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        Long aLong = Long.valueOf(claims.getId());
        log.info("获取登录token{}:",subject);
        return Result.OK();
    }

    // 收藏的页面展示
    @GetMapping
    @ApiOperation("查询全部的收藏车辆接口")
    public Result getCollection(){
        List<Conllection> list = collectionService.getCollection();
        return Result.OK().setData(list);
    }

    // 收藏页面的编辑
    @DeleteMapping
    public Result deleteCollection(@RequestParam List<Long> ids){
        collectionService.deleteCollection(ids);
        return Result.OK();
    }

    // 收藏页面的为你推荐
    @GetMapping
    public Result findRecommend(@RequestParam List<Long> ids){
        collectionService.findRecommend(ids);
        return Result.OK();
    }
}
